////////////////////////////////////////////////////////////////////////////////
// Copyright 2006 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   ACCESS_RIGHT
// AUTHOR           :   Victor Zheng 
// DATE CREATE      :   2006/02/01
// DESCRIPTION      :   a MAINTENANCE FORM file for the function, ACCESS_RIGHT
//                  :   
//                  :   
//                  :   
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2006/02/01   Victor Zheng    1.0                 source is generated.
//
//
//
////////////////////////////////////////////////////////////////////////////////

package com.erp.app.admin.accessRight.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.erp.framework.controller.form.SimpleMaintStyleForm;
import com.erp.framework.util.logger.Logger;

import com.erp.app.common.ProjectConstants;
import com.erp.app.common.util.ProjectCommonUtil;
import com.erp.app.util.web.ActionErrorUtil;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.model.schema.TableSchema;

/**
* <p>A <strong>MaintenanceForm</strong> is a JavaBean which provides property
* getter and setter methods for all of the bean properties, and implements
* <code>Serializable</code>, as required by the JavaBean specification
* The main purpose for this bean is to get the user input
* from the web browsers.
*
* @author          Victor Zheng 
* @version 1.0     2006/02/01
* @since           FrameWork 1.0
*
*/
public class AccessRightMaintenanceForm extends SimpleMaintStyleForm {

	//Header Attributes (DB Table Name: T_ACCESS_RIGHT_HDR)
	private String companyCode;                      //DB Column Name: COMPANY_CODE
	private String userGroupCode;                    //DB Column Name: USER_GROUP_CODE
	private String userGroupDesc;                    //DB Column Name: USER_GROUP_DESC
	private String status;                           //DB Column Name: STATUS
	private String createUser;                       //DB Column Name: CREATE_USER
	private String createDate;                       //DB Column Name: CREATE_DATE
	private String updateUser;                       //DB Column Name: UPDATE_USER
	private String updateDate;                       //DB Column Name: UPDATE_DATE

	//Child Attributes (DB Table Name: T_ACCESS_RIGHT_DTL)
	private String[] dtlLineNo;                      //DB Column Name: LINE_NO
	private String[] dtlDept;                        //DB Column Name: DEPT
	private String[] dtlUserCode;                    //DB Column Name: USER_CODE
	private String[] dtlStatus;                      //DB Column Name: STATUS

	/**
	* Validate the properties that have been set for this non-HTTP request,
	* and return an <code>ActionErrors</code> object that encapsulates any
	* validation errors that have been found.  If no errors are found, return
	* <code>null</code> or an <code>ActionErrors</code> object with no
	* recorded error messages.
	* <p>
	* The default implementation attempts to forward to the HTTP version of
	* this method.
	*
	* @param      the action errors array
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	public void otherValidate(ActionErrors errors) {

		validateHdr(errors);
		validateDtl(errors);
	}

	/**
	* Validate the properties that have been set for this non-HTTP request,
	* and return an <code>ActionErrors</code> object that encapsulates any
	* validation errors that have been found.  If no errors are found, return
	* <code>null</code> or an <code>ActionErrors</code> object with no
	* recorded error messages.
	* <p>
	* The default implementation attempts to forward to the HTTP version of
	* this method.
	*
	* @param      errors The errors array that is used for storing validation errors
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	public void validateHdr(ActionErrors errors) {

        TableSchema ts = getTs();
        String validateFeildName = new String();

        validateFeildName = ts.getDisplayWebLabelByObjectId("LBL_HDR_USER_GROUP_CODE");
        if (CommonUtil.isEmpty(userGroupCode)){
            ActionErrorUtil.addErrorForGeneralFieldBlank(errors, validateFeildName);
        }
        validateFeildName = ts.getDisplayWebLabelByObjectId("LBL_HDR_STATUS");
        if (CommonUtil.isEmpty(status)){
            ActionErrorUtil.addErrorForGeneralFieldBlank(errors, validateFeildName);
        }

        
        
        /*
		TableSchema ts = getTs();
		if (CommonUtil.isEmpty(companyCode)) {
			ActionErrorUtil.addErrorForGeneralFieldBlank(errors, ts.getDisplayWebLabelByObjectId("LBL_HDR_COMPANY_CODE"));
		}
		*/

		/*
		if (CommonUtil.isEmpty(issueDate)) {
			ActionErrorUtil.addErrorForGeneralFieldBlank(errors, ts.getDisplayWebLabelByObjectId("LBL_HDR_ISSUE_DATE"));
		} else {
			if (!CommonUtil.isValidDate(issueDate)) {
				ActionErrorUtil.addErrorForInvalidDate(errors, ts.getDisplayWebLabelByObjectId("LBL_HDR_ISSUE_DATE"));
			}
		}
		*/
		
	}

	/**
	* Validate the properties that have been set for this non-HTTP request,
	* and return an <code>ActionErrors</code> object that encapsulates any
	* validation errors that have been found.  If no errors are found, return
	* <code>null</code> or an <code>ActionErrors</code> object with no
	* recorded error messages.
	* <p>
	* The default implementation attempts to forward to the HTTP version of
	* this method.
	*
	* @param      errors The errors array that is used for storing validation errors
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	public void validateDtl(ActionErrors errors) {

        TableSchema[] childTsArray = getChildTs();
        TableSchema childTs = childTsArray[AccessRightMaintenanceAction.TAB_IDX_DTL];

        String validateFeildName = new String();

		/*
		TableSchema ts = getTs();
        */
		ArrayList dtlRowList = getMultiRows(AccessRightMaintenanceAction.TAB_IDX_DTL);

		HashMap deptMap = new HashMap();
		
		for (int i = 0; (dtlRowList != null) && (i < dtlRowList.size()); i++) {
			AccessRightDtlRow row = (AccessRightDtlRow) dtlRowList.get(i);
            
            if (!CommonUtil.isEmpty(row.getIsDelete()))
                continue;
            
            if (CommonUtil.isEmpty(row.getDtlLineNo())){
                validateFeildName = childTs.getDisplayWebLabelByObjectId("LBL_DTL_LINE_NO");
                ActionErrorUtil.addErrorForGeneralFieldBlank(errors, validateFeildName);
            }
            if (CommonUtil.isEmpty(row.getDtlDept())){
                validateFeildName = childTs.getDisplayWebLabelByObjectId("LBL_DTL_DEPT");
                ActionErrorUtil.addErrorForGeneralFieldBlank(errors, validateFeildName);
            }
            if (CommonUtil.isEmpty(row.getDtlUserCode())){
                validateFeildName = childTs.getDisplayWebLabelByObjectId("LBL_DTL_USER_CODE");
                ActionErrorUtil.addErrorForGeneralFieldBlank(errors, validateFeildName);
            }
            
            if (errors.size() == 0) {
	            if (deptMap.containsKey(row.getDtlDept())) {
	            	ActionErrorUtil.addErrorForSpecficPurpose(errors, "Duplicate Department is not allowed - "+row.getDtlDept());
	            } else {
	            	deptMap.put(row.getDtlDept(), row.getDtlDept());
	            }
            }
		}		
	}

	/**
	* Validate the properties that have been set for this non-HTTP request,
	* and return an <code>ActionErrors</code> object that encapsulates any
	* validation errors that have been found.  If no errors are found, return
	* <code>null</code> or an <code>ActionErrors</code> object with no
	* recorded error messages.
	* <p>
	* The default implementation attempts to forward to the HTTP version of
	* this method.
	*
	* @param      mapping      the action mapping
	* @param      formIn       the action form
	* @param      request      the http request
	* @param      response     the http response
	* @param      errors       the action errors array
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	public void moreValidate(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		AccessRightMaintenanceForm form = (AccessRightMaintenanceForm) formIn;

		try {

		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	/**
	* Reset all bean properties to their default state.  This method
	* is called inside the reset() method.
	*
	* @param      mapping The mapping used to select this instance
	* @param      request The servlet request we are processing
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	public void otherReset(ActionMapping mapping, HttpServletRequest request) {
		
	}

	/**
	* Converts to a string representing the data in this object.
	* A new <code>String</code> object is allocated and initialized to 
	* contain the character sequence currently represented by this 
	* object. This <code>String</code> is then returned. Subsequent 
	* changes to this object do not affect the contents of the 
	* <code>String</code>. 
	*
	* @return     a string representation of the object.
	* @see        java.lang.Object#toString()
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	public String toString() {
		String pkStr = "PK: {companyCode = " + this.companyCode
			+ "\n, userGroupCode = " + this.userGroupCode + "}\n";
		String colStr = "COL: {userGroupDesc = " + this.userGroupDesc
			+ "\n, status = " + this.status
			+ "\n, createUser = " + this.createUser
			+ "\n, createDate = " + this.createDate
			+ "\n, updateUser = " + this.updateUser
			+ "\n, updateDate = " + this.updateDate + "}\n";
		return "AccessRightMaintenanceForm.toString() (header only) is:\n" + pkStr + colStr;
	}

	////////////////////////////////////////////////////////////////////////////////
	//Generated Accessors (get/set methods) for Header (AccessRightHdr)
	////////////////////////////////////////////////////////////////////////////////

	public String getCompanyCode() {
		return this.companyCode;
	}

	public String getUserGroupCode() {
		return this.userGroupCode;
	}

	public String getUserGroupDesc() {
		return this.userGroupDesc;
	}

	public String getStatus() {
		return this.status;
	}

	public String getCreateUser() {
		return this.createUser;
	}

	public String getCreateDate() {
		return this.createDate;
	}

	public String getUpdateUser() {
		return this.updateUser;
	}

	public String getUpdateDate() {
		return this.updateDate;
	}

	public void setCompanyCode(String companyCode) {
		this.companyCode = companyCode;
	}

	public void setUserGroupCode(String userGroupCode) {
		this.userGroupCode = ProjectCommonUtil.toCode(userGroupCode);
	}

	public void setUserGroupDesc(String userGroupDesc) {
		this.userGroupDesc = userGroupDesc;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public void setCreateUser(String createUser) {
		this.createUser = createUser;
	}

	public void setCreateDate(String createDate) {
		this.createDate = createDate;
	}

	public void setUpdateUser(String updateUser) {
		this.updateUser = updateUser;
	}

	public void setUpdateDate(String updateDate) {
		this.updateDate = updateDate;
	}

	////////////////////////////////////////////////////////////////////////////////
	//Generated Accessors (get/set methods) for Child (AccessRightDtl)
	////////////////////////////////////////////////////////////////////////////////

	public String[] getDtlLineNo() {
		return this.dtlLineNo;
	}

	public String[] getDtlDept() {
		return this.dtlDept;
	}

	public String[] getDtlUserCode() {
		return this.dtlUserCode;
	}

	public String[] getDtlStatus() {
		return this.dtlStatus;
	}

	public void setDtlLineNo(String[] dtlLineNo) {
		this.dtlLineNo = dtlLineNo;
	}

	public void setDtlDept(String[] dtlDept) {
		this.dtlDept = dtlDept;
	}

	public void setDtlUserCode(String[] dtlUserCode) {
		this.dtlUserCode = dtlUserCode;
	}

	public void setDtlStatus(String[] dtlStatus) {
		this.dtlStatus = dtlStatus;
	}

	////////////////////////////////////////////////////////////////////////////////
	//Other Accessors (get/set methods)
	////////////////////////////////////////////////////////////////////////////////

	public Iterator getStatusValues(){
		return this.getTemplate().getCommonStatusValues();		
	}
	

}